<?php
/**
 * 资金流水
 * Created by PhpStorm.
 * User: 王海东
 * Date: 2018/12/25
 * Time: 14:09
 */

namespace app\adm\controller;

use app\common\model\CapitalLog;
use app\common\model\Capital;
use app\common\model\DepositWithdraw;

use think\Db;

class CapitalController extends BaseController
{
    /**
     * 资金流水
     * @author 王海东
     * @date
     * @return void
     * @throws \Exception
     */
    public function capitalList()
    {
        $in = $this->I([
            [
                "start_time|开始时间",
                null,
                "s",
            ],
            [
                "end_time|截止时间",
                null,
                "s",
            ],
            [
                "name|用户名或手机号",
                null,
                "s",
            ],
            [
                "agent_name|代理商名称",
                null,
                "s",
            ],
            [
                "type|资金变动类型",  //1充值 2提现 3买入 4卖出 5递延费
                null,
                "d",
            ],
            [
                "page|页码",
                DEF_PAGE_NO,
                "d",
            ],
            [
                "pageSize|页幅",
                DEF_PAGE_SIZE,
                "d",
            ]
        ]);


        $cl = new CapitalLog();

        $map = [
          'start_time'  => $in['start_time'],
          'end_time'    => $in['end_time'],
          'name'        => $in['name'],
          'agent_name'  => $in['agent_name'],
          'type'        => $in['type'],
        ];

        $data = $cl->select(UID,$map,$in['page'],$in['pageSize']);

        rjd($data);
    }

    /**
     * 出入金记录
     * @author 王海东
     * @date
     * @return void
     * @throws \Exception
     */
    public function cashList()
    {
        $in = $this->I([
            [
                "start_time|开始时间",
                null,
                "s",
            ],
            [
                "end_time|截止时间",
                null,
                "s",
            ],
            [
                "name|用户名或手机号",
                null,
                "s",
            ],
            [
                "agent_name|代理商名称",
                null,
                "s",
            ],
            [
                "status|提现状态",  //1，处理中；2，成功；3，失败; 4：审核成功（仅对提现）; 5：审核失败（仅对提现）
                null,
                "d",
            ],
            [
                "type|订单类型",  //1充值 2提现
                null,
                "d",
                "require"
            ],
            [
                "trade_no|订单号",
                null,
                "s",
            ],
            [
                "page|页码",
                DEF_PAGE_NO,
                "d",
            ],
            [
                "pageSize|页幅",
                DEF_PAGE_SIZE,
                "d",
            ]
        ]);

        $map = [
            'start_time'    => $in['start_time'],
            'end_time'      => $in['end_time'],
            'name'          => $in['name'],
            'agent_name'    => $in['agent_name'],
            'status'        => $in['status'],
            'type'          => $in['type'],
            'trade_no'      => $in['trade_no'],
        ];

        $dw     = new DepositWithdraw();
        $data   = $dw->select(1,$map,$in['page'],$in['pageSize']);

        rjd($data);
    }

    /**
     * 修改提现状态
     */
    public function editWithdraw()
    {
        $in = $this->I([
            [
                "status|提现状态",  //4成功 5失败
                null,
                "d",
                "require"
            ],
            [
                "id|提现编号",
                null,
                "d",
                "require"
            ]
        ]);

        if ($in['status'] != 4 && $in['status'] != 5) {
            rj(1,'审核状态不正确');
        }

        Db::startTrans();

        try {
            $dw     = new DepositWithdraw();
            if(!$dw->examine($in['id'],$in['status'])) {
                Db::rollback();
                rj(1,'审核失败');
            }

            //审核失败
            if($in['status'] == 5) {

                $c  = new Capital();
                $cl = new CapitalLog();

                $info   = $dw->find($in['id']);

                $c_res  = $c->setBalance($info['user_id'],$info['amount']);
                $cl_res = $cl->add($info['user_id'],$info['amount'],'提现审核失败退回金额【'.$info['amount'].'】元',2);
                if(!$c_res || !$cl_res) {
                    Db::rollback();
                    rj(1,'审核失败');
                }
            }

            Db::commit();
            rj();

        } catch (\Exception $e) {
            Db::rollback();
            rj(1,'审核失败');
        }
    }
}